/**
 * @method Countdown 验证码倒计时解决方案
 * @author Lkx
 * @for Kxui
 * @for method
 *
 * @method use 开始使用
 */

(function (factory) {

  if ((typeof exports === 'object') && (typeof module !== 'undefined')) {
    module.exports = factory();
  } else {
    window.Kxui.countdown = factory();
  }

}(function () {

  'use strict';

  let Kxui = window.Kxui;

  /**
   * 逻辑层
   * @method Logic
   * @for countdown
   * @param {object} parameter 配置参数
   */
  let Logic = function (parameter) {
    this.parameter = (typeof parameter === 'object') ? parameter : {};
    this.init();
  };

  Logic.prototype = {

    /**
     * 初始化
     * @method init
     * @for Logic
     */
    init: function () {
      this.el = Kxui.method.getDom(this.parameter.el);
      if (!this.parameter.el) {
        throws(0, 0);
      } else if (!this.el) {
        throws(1, 1, this.parameter.el);
      } else {
        this.variable();
      }
    },

    /**
     * 变量生成
     * @method variable
     * @for init
     */
    variable: function () {
      this.inOn = Kxui.method.atrDom(this.el, 'kxui-countdown-on');
      this.disabled = (typeof this.parameter.disabled === 'boolean' ? this.parameter.disabled : false);
      this.time = +this.parameter.time || 120;
      this.text = (typeof this.parameter.text === 'string' ? this.parameter.text : false);
      this.endText = this.parameter.endText || '重新获取';
      this.callback = (typeof this.parameter.callback === 'function' ? this.parameter.callback : function () {});

      if (this.inOn === '2') {
        let disabled = Kxui.method.hasClass(this.el, 'kxui-countdown-disabled');
        let officialDisabled = Kxui.method.atrDom(this.el, 'disabled');
        if (this.disabled && !disabled && !officialDisabled) {
          Kxui.method.atrDom(that.el, 'disabled', true);
          Kxui.method.addClass(this.el, 'kxui-countdown-disabled');
        } else if (!this.disabled) {
          this.callback(false);
        }
      } else {
        this.loop();
        this.reciprocal();
        this.callback(true);
      }
    },

    /**
     * 可循环赋值
     * @method loop
     * @for variable
     */
    loop: function () {
      let text = '';
      if (this.text && this.text.indexOf('#T') >= 0) {
        text = Kxui.method.repStr(this.text, '#T', this.time);
      } else if (this.text) {
        text = this.text;
      } else {
        text = this.time;
      }
      this.el.innerHTML = text;
    },

    /**
     * 进行倒数事件
     * @method reciprocal
     * @for variable
     */
    reciprocal: function () {
      let that = this;
      if (this.disabled) {
        Kxui.method.atrDom(that.el, 'disabled', true);
        Kxui.method.addClass(this.el, 'kxui-countdown-disabled');
      }

      Kxui.method.atrDom(that.el, 'kxui-countdown-on', '2');
      that.timer = setInterval(function () {
        that.time = that.time - 1;
        if (that.time > 0) {
          that.loop();
        } else {
          clearInterval(that.timer);
          that.el.innerHTML = that.endText;
          Kxui.method.atrDom(that.el, 'kxui-countdown-on', '1');
          if (that.disabled) {
            Kxui.method.atrDom(that.el, 'disabled', false);
            Kxui.method.delClass(that.el, 'kxui-countdown-disabled');
          }
        }
      }, 1000);
    }
  };

  /**
   * 控制台错误/警告
   * @method throws
   * @for Logic
   * @param {number} error 抛出错误的类型 0:警告 1:错误
   * @param {number} num 输入警告文案编号
   * @param {string/boolean} dome 发生错误的对象名称
   */
  function throws(error, num, dome) {
    if (Kxui.configuration.debug) {
      let nums = {};
      nums[0] = '配置参数 {el} 不能为空';
      nums[1] = '无法找到 {' + dome + '} 节点或存在多个 {' + dome + '} 节点';
      let text = 'Kxui-' + Kxui.about.version + '： 模块 {countdown} ' + nums[num] + '，请检查修改。';
      error === 1 ? console.error(text) : console.warn(text);
    }
  }

  /**
   * 验证码倒计时解决方案
   * @method Countdown
   */
  let Countdown = function () {
    this.name = 'Countdown';
    this.info = 'Validation Code Countdown Solution';
  };

  Countdown.fn = Countdown.prototype;

  /**
   * 开始使用
   * @method use
   * @for Countdown
   * @param {object} parameter 配置参数
   */
  Countdown.fn.use = function (parameter) {
    new Logic(parameter);
  };

  return new Countdown();

}));